﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YoucaiReader.Entities;

/**
 * 创建时间：2014/10/12 10:06:53
 * 作者：yong.blackcore
 * Email：itblackhole@outlook.com
 * QQ：50404503
 */
namespace YoucaiReader.ViewModels
{
    internal class ChapterInfoListViewModel
    {
        private ObservableCollection<BookChapterListModel> _T_ChapterInfoList;
        public ObservableCollection<BookChapterListModel> T_ChapterInfoListViewModel()
        {
            if (this._T_ChapterInfoList == null)
            {
                this._T_ChapterInfoList = new ObservableCollection<BookChapterListModel>();
                if (App.QQReader_StorageFile.IsFile(App.ReaderFileName + "chapterList"))
                {
                    IsolatedStorageFileStream stream = App.QQReader_StorageFile.StorageFile.OpenFile(App.ReaderFileName + "chapterList", FileMode.Open, FileAccess.Read);
                    byte[] buffer = new byte[stream.Length];
                    stream.Read(buffer, 0, (int)stream.Length);
                    stream.Flush();
                    stream.Close();
                    string str = Encoding.UTF8.GetString(buffer, 0, buffer.Length);
                    do
                    {
                        int index = str.IndexOf('\n', 0);
                        char[] destination = new char[index];
                        str.CopyTo(0, destination, 0, index);
                        str = str.Remove(0, index + 1);
                        index = str.IndexOf('\r', 0);
                        char[] chArray2 = new char[index];
                        str.CopyTo(0, chArray2, 0, index);
                        str = str.Remove(0, index + 1);
                        BookChapterListModel item = new BookChapterListModel
                        {
                            ChapterList_Title = new string(destination),
                            ChapterList_Index = int.Parse(new string(chArray2))
                        };
                        this._T_ChapterInfoList.Add(item);
                    }
                    while (str.Length > 0);
                }
            }
            return this._T_ChapterInfoList;
        }
    }
}
